//全局变量
var table=null,where={};

//监听事件
layui.use(['table', 'jquery','form', 'admin'], function(){
    table = layui.table,
        $ = layui.jquery,
        form = layui.form,
        admin = layui.admin;

    //加载数据
    loadData();

    //监听提交
    form.on('submit(search)', function(data){
        where=data.field;
        loadData();
        return false;
    });

    //头工具栏事件
    table.on('toolbar(tblList)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            /*case 'getCheckData':
             var data = checkStatus.data;
             layer.alert(JSON.stringify(data));
             break;*/
            case 'add':
                WeAdminShow('添加','/admin/{subFolder}/{tblName}add',600,400);
                break;
        };
    });

    //监听行工具事件
    table.on('tool(tblList)', function(obj){
        var data = obj.data;
        console.log(obj)
        if(obj.event === 'del'){
            layer.confirm('确定要删除此记录吗？删除后不可恢复！', function(index){
                doDelete(obj.data.id);
                //obj.del();
                layer.close(index);
            });
        } else if(obj.event === 'edit'){
            WeAdminShow('修改','/admin/{subFolder}/{tblName}add/?id='+obj.data.id,600,400);

        }
    });
});

//加载数据
function loadData() {
    var url="/admin/{subFolder}/{tblName}list";
    table.render({
        elem: '#tblList',
        method: 'post'
        ,url:url
        ,toolbar: '#toolbarDemo'
        ,title: '用户数据表'
        ,where: where
        ,cols: [[
            //{type: 'checkbox', fixed: 'left'},
			{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
		  [loop start]
            ,{field:'[fldName]', title:'[fldTitle]', width:120}
		  [loop end]^ 
            ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
        ]]
        ,page: true
    });
}

//删除记录
function doDelete(id) {
    var url = "/admin/{subFolder}/{tblName}delete";
    $.ajax({
            type: "post",
            url: url,
            data: { id:id },
            dataType: "json",
            error: function (request) {
                ajaxError(request, $(this));
            },
            success: function (result) {
                ajaxSuccess(result, $(this));
                var code=result.code;
                if(code==0){
                    loadData();
                }else{
                    layer.alert("提交失败："+result.msg);
                }
            }
        }
    );

}
